package com.sneaker.shower.controller;

import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.entity.dto.CartDTO;
import com.sneaker.shower.entity.dto.GoodsItemDTO;
import com.sneaker.shower.service.CartService;
import com.sneaker.shower.util.JacksonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "购物车Api")
@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {

    private final CartService cartService;

    @Autowired
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public BaseView<Long> add(@RequestBody List<GoodsItemDTO> goodsItemDTOList) {
        if (log.isInfoEnabled()) {
            log.info("加入购物车请求入参：【{}】", JacksonUtils.writeValueAsString(goodsItemDTOList));
        }
        cartService.add(goodsItemDTOList);
        return new BaseView<>();
    }

    @GetMapping("/remove/{guid}")
    @ApiOperation("删除购物车")
    public BaseView remove(@PathVariable("guid") Long guid) {
        log.info("删除购物车商品请求入参【{}】", guid);
        return new BaseView<>(cartService.remove(guid));
    }

    @GetMapping("/remove_all")
    @ApiOperation("清空购物车")
    public BaseView removeAll() {
        log.info("清空购物车请求");
        cartService.removeAll();
        return new BaseView();
    }

    @GetMapping("/get_cart")
    @ApiOperation("查询购物车详情")
    public BaseView<CartDTO> getCart() {
        log.info("查询购物车请求");
        return new BaseView<>(cartService.getCart());
    }

    @GetMapping("/count")
    public BaseView<Integer> getCount() {
        log.info("查询购物车数量请求");
        return new BaseView<>(cartService.getCount());
    }
}
